﻿import java.io.*;
import java.net.MalformedURLException;
//import java.text.SimpleDateFormat;
//import java.util.Calendar;
//import java.awt.*;
import java.util.*;

//import com.itextpdf.text.Paragraph;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;

public class PDFSchreiben {
	// private static SimpleDateFormat formater = new SimpleDateFormat();
	private String a1 = "";
	private String a2 = "";
	private String a3 = "";
	private String a4 = "";
	private String a5 = "";
	private String b1 = "";
	private String b2 = "";
	private String b3 = "";
	private String b4 = "";
	private String b5 = "";
	private String b6 = "";
	private String b7 = "";
	private String b8 = "";
	private String b9 = "";
	private java.awt.Image ii1 = null;
//	private java.awt.Image ii2 = null;
	private Vector<String> v = null;

	public static final String PDF = "D:\\document.pdf";

	public void initData(String s1, String s2, String s3, String s4, String s5, java.awt.Image i1,
			String t1, String t2, String t3, String t4, String t5, String t6, String t7, String t8, String t9,
			Vector<String> v) {
		a1 = s1;
		a2 = s2;
		a3 = s3;
		a4 = s4;
		a5 = s5;
		b1 = t1;
		b2 = t2;
		b3 = t3;
		b4 = t4;
		b5 = t5;
		b6 = t6;
		b7 = t7;
		b8 = t8;
		b9 = t9;
		ii1 = i1;
		System.out.println("DEBUG >JAN>" + v.size());
		for (int x = 0; x < v.size(); x++) {
			System.out.println("DEBUG >JAN>" + v.elementAt(x));
		}
		this.v = v;
	}

	public void createPdf(String filename) throws DocumentException, IOException {
		Document document = new Document(PageSize.A4);
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));

		document.open();
		PdfContentByte cb = writer.getDirectContent();
		BaseFont bf = BaseFont.createFont();
//		setImage(writer, cb, 40);
		cb.beginText();
		cb.setFontAndSize(bf, 40);
		cb.moveText(40, 780);
		cb.showText("*** Einsatzdepeche ***");
		cb.setFontAndSize(bf, 20);
		cb.newlineShowText("Meldung: ");
		cb.setFontAndSize(bf, 12);
		cb.moveText(10, -50);
		cb.setCharacterSpacing(0);
		cb.setWordSpacing(5);
		cb.newlineShowText(a1);
		cb.moveText(0, -15);
		cb.newlineShowText(a2);
		cb.moveText(0, -15);
		cb.newlineShowText(a3);
		cb.moveText(0, -15);
		cb.newlineShowText(a4);
		cb.moveText(0, -15);
		cb.newlineShowText(a5);
		cb.moveText(0, -80);
		cb.setFontAndSize(bf, 20);
		cb.newlineShowText("Fahrzeuge: ");
		cb.setFontAndSize(bf, 12);
		cb.moveText(0, -15);
		cb.newlineShowText(b1);
		cb.moveText(0, -15);
		cb.newlineShowText(b2);
		cb.moveText(0, -15);
		cb.newlineShowText(b3);
		cb.moveText(0, -15);
		cb.newlineShowText(b4);
		cb.moveText(0, -15);
		cb.newlineShowText(b5);
		cb.moveText(0, -15);
		cb.newlineShowText(b6);
		cb.moveText(0, -15);
		cb.newlineShowText(b7);
		cb.moveText(0, -15);
		cb.newlineShowText(b8);
		cb.moveText(0, -15);
		cb.newlineShowText(b9);
		cb.moveText(0, -200);
		cb.setFontAndSize(bf, 12);
		cb.setWordSpacing(5);
		for (int x = 0; x < v.size(); x++) {
			String htmlString = ((String) v.elementAt(x));//.replaceAll("<[^<]+>|\\[|\\]", "");
			StringReader strReader = new StringReader(htmlString);
			java.util.List<Element> p = HTMLWorker.parseToList(strReader, null);
			for (Element eachElement : p) {
				cb.newlineShowText(eachElement.toString().replaceAll("<[^<]+>|\\[|\\]", ""));
				System.out.println("####PDF : "+eachElement.toString().replaceAll("<[^<]+>|\\[|\\]", ""));
				cb.moveText(0, -15);
			}
			/*
			StringReader strReader = new StringReader(htmlString);
			java.util.List<Element> p = HTMLWorker.parseToList(strReader, null);

			Paragraph paragraph = new Paragraph();

			for (Element eachElement : p) {
				paragraph.add(eachElement.toString().replaceAll("<[^<]+>|\\[|\\]", ""));
			}
			document.add(paragraph);
			*/
		}
		cb.endText();
		document.close();
	}

	private void setImage(PdfWriter writer, PdfContentByte cb, float scalePercent) throws MalformedURLException,
			IOException, DocumentException {
		Image img1 = Image.getInstance(writer, ii1, 1);
		img1.scalePercent(60);
		// img1.setAbsolutePosition(300, 300);
		img1.setAbsolutePosition(180, 180);
		cb.addImage(img1);
	}

	private void printMeasures() {
		System.out.println("A4-Ma\u00DFe: " + PageSize.A4.getWidth() + "pt x " + PageSize.A4.getHeight() + "pt - "
				+ (PageSize.A4.getWidth() * 0.3527) + "mm x " + (PageSize.A4.getHeight() * 0.3527) + "mm");
	}

	public static void main(String[] args) throws DocumentException, IOException {
		PDFSchreiben ps = new PDFSchreiben();
		ps.createPdf(PDF);
		ps.printMeasures();
		System.out.println("done");
	}
}